package cac.gkinterface.rest;

import cac.common.CallInterFace.service.CallInterfaceService;
import cac.common.CreatMsgId;
import cac.gkinterface.dao.WorkPackageDao;
import cac.gkinterface.service.WorkService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import net.logstash.logback.encoder.org.apache.commons.lang.exception.ExceptionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import static cac.common.DateUtil.*;

/**
 * @className: SearchErp
 * @description: （描述这个类的作用）
 * @author: lqc
 * @date: 2023/5/9 16:22:22 周二
 * @version: 1.0
 */
@RestController
@RequestMapping("/SearchErp")
@Slf4j
@EnableScheduling
public class SearchErp {

    @Autowired
    private WorkService workService;
    @Autowired
    private WorkPackageDao workPackageDao;
    @Autowired
    private CallInterfaceService callInterfaceService;
    @Autowired
    private Environment environment;

    //查询erp处理结果
    @GetMapping("/SearchErpresult")
    @Scheduled(cron = "0 0/30 * * * ?")
    public void SearchErp() {

        String isReturnERP = environment.getProperty("pathProperty.isReturnERP");//yebo 更改参数名字
        if ("false".equals(isReturnERP)) {
            return;
        }
        log.info("定时查询erp处理结果接口开始调用！");
        HashMap issuedTest = new HashMap();
        String rstl = "";
        try {
            //获取当前时间
            Date date = new Date();
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            System.out.println("date：" + dateFormat.format(date));
            String msgId = CreatMsgId.getMsgId();
            //查询当天无返回结果的消息id
            List<HashMap> msgIds = workPackageDao.searchMsgId();
            //查询结果无不调接口
            if(msgIds.size()<=0){
                return;
            }
            log.info("集合：" + msgIds.toString());
            HashMap BData = new HashMap();
            BData.put("data", msgIds);
            BData.put("msgId", msgId);
            BData.put("func", "101-ERP-MSGSTATUS");

            log.info("查询ERP处理结果！");
            log.info("BData+++++++++++++" + JSON.toJSONString(BData));
            callInterfaceService.sendBData("sendBData", "两网传输-发送业务数据",
                    CallInterfaceService.encodeBData(JSON.toJSONString(BData)), "查询ERP处理结果");
//            issuedTest.put("id", LogId);
            issuedTest.put("BData", BData.toString());
            issuedTest.put("type", "101-ERP-MSGSTATUS");
            issuedTest.put("msgId", msgId);
            issuedTest.put("retCode", "1");
        } catch (Exception e) {
            e.printStackTrace();
            rstl = ExceptionUtils.getFullStackTrace(e);
            rstl = rstl.substring(0, 1999);
            issuedTest.put("result", rstl);
            issuedTest.put("retCode", "0");
        } finally {
            issuedTest.put("failMsg", rstl);
            workService.issuedTest(issuedTest);
        }


    }


    //定时重推
    @GetMapping("/repeatSendErp")
    @Scheduled(cron = "0 1 0-23 * * ?")
    public void repeatSendERP() {
        String isReturnERP = environment.getProperty("pathProperty.isReturnERP");
        if ("false".equals(isReturnERP)) {
            return;
        }
        //查询当天无返回结果的消息id
        HashMap issuedTest = new HashMap();
        String rstl = "";
        try {
            List<HashMap> data = workPackageDao.searchBData();
            for (HashMap Data : data) {

                callInterfaceService.sendBData("sendBData", "两网传输-发送业务数据",
                        CallInterfaceService.encodeBData(JSON.toJSONString(JSON.parseObject((String)Data.get("BData")))), "定时重推");
                issuedTest.put("BData", Data.get("BData").toString());
                issuedTest.put("type", "101-ERP-RCVMSG");
                issuedTest.put("msgId", Data.get("msgId"));
                issuedTest.put("retCode", "1");
                issuedTest.put("remark", "Scheduled");
                workService.issuedTest(issuedTest);
            }
        } catch (Exception e) {
            e.printStackTrace();
            rstl = ExceptionUtils.getFullStackTrace(e);
            rstl = rstl.substring(0, 1999);
            issuedTest.put("result", rstl);
            issuedTest.put("retCode", "0");
        } finally {
            if (rstl.length() > 10) {
                issuedTest.put("failMsg", rstl);
                workService.issuedTest(issuedTest);
            }
        }

    }
    @GetMapping("test")
    public String test(){
        HashMap map = new HashMap();
        String a = null;
        map.put("ab","1223");
        if(a==null){
            map.put("hgzNo","");
        }else{
            map.put("hgzNo",a);
        }
        JSONObject jsonObject = new JSONObject(map);
        System.out.println("================="+jsonObject.toJSONString());
        return "ok";
    }

}
